package com.ruoyi.car.business.web.controller;

import com.ruoyi.car.business.domain.BusBpmnInfo;
import com.ruoyi.car.business.domain.BusServiceItem;
import com.ruoyi.car.business.query.ServiceItemQuery;
import com.ruoyi.car.business.service.IBusBpmnInfoService;
import com.ruoyi.car.business.service.IBusServiceItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 服务单项Controller
 */
@Controller
@RequestMapping("business/serviceItem")
public class BusServiceItemController {
    //模板前缀
    private static final String prefix = "business/serviceItem/";

    @Autowired
    private IBusServiceItemService serviceItemService;
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;
    @Autowired
    private SysDictDataServiceImpl dictDataService;
    @Autowired
    private ISysUserService userService;

    //列表
    @RequiresPermissions("business:serviceItem:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:serviceItem:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }


    @RequiresPermissions("business:serviceItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("serviceItem", serviceItemService.get(id));
        return prefix + "edit";
    }

    //列表
    @RequiresPermissions("business:serviceItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BusServiceItem> query(ServiceItemQuery qo) {
        return serviceItemService.query(qo);
    }

    //新增
    @RequiresPermissions("business:serviceItem:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusServiceItem serviceItem) {
        serviceItemService.save(serviceItem);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:serviceItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BusServiceItem serviceItem) {
        serviceItemService.update(serviceItem);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:serviceItem:remove")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult remove(BusServiceItem serviceItem) {
        serviceItemService.deleteBatch(serviceItem);
        return AjaxResult.success();
    }

    //上架
    @RequestMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOn(Long id) {
        serviceItemService.saleOn(id);
        return AjaxResult.success();
    }

    // 下架
    @RequestMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id) {
        serviceItemService.saleOff(id);
        return AjaxResult.success();
    }

    //服务套餐发起审核
    @RequestMapping("/auditPage")
    public String auditPage(Model model, Long id) {
        model.addAttribute("serviceItem", serviceItemService.get(id));
        // 套餐审核流程
        BusBpmnInfo bpmnInfo = busBpmnInfoService.getByBpmnType("car_package");
        bpmnInfo.setBpmnName(dictDataService.selectDictLabel("bus_audit_type","car_package"));
        model.addAttribute("bpmnInfo", bpmnInfo);
        //店长
        model.addAttribute("directors", userService.queryByRoleKey("shopOwner"));
        //财务
        model.addAttribute("finances", userService.queryByRoleKey("financial"));
        return prefix + "audit";
    }

    // 发起审核 启动流程请求
    @RequestMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(Long id, Long bpmnInfoId,  Long director, Long finance, String info){
        serviceItemService.startAudit(id, bpmnInfoId, director, finance,info);
        return AjaxResult.success();
    }
}
